package com.zj.juc.study.completion;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 自己的名字
 * @version 1.0.0
 * @ClassName CompletableFutureMallDemo
 * @Description
 * @createTime 2024年12月21日 15:45
 */
public class CompletableFutureMallDemo {

    static List<NetMall> MALL_LIST = Arrays.asList(
            new NetMall("jd"),
            new NetMall("dd"),
            new NetMall("tb")
    );

    public static void main(String[] args) {
        getPriceByCompletableFuture(MALL_LIST,"mysql").forEach(System.out::println);
    }

    private static List<String> getPriceByCompletableFuture(
            List<NetMall> netMallList,
            String productName)
    {
        return netMallList.stream()
                .map(netMall ->
                CompletableFuture.supplyAsync(
                        () -> String.format(
                                productName + " in %s price is %.2f",
                                netMall.getNetMallName(),
                                netMall.getPrice(productName)
                        )
                )
        ).collect(Collectors.toList())
                .stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
    }


}

class NetMall{

    @Getter
    private String netMallName;

    public NetMall(String netMallName){
        this.netMallName = netMallName;
    }

    public double getPrice(String pruductName){
        return ThreadLocalRandom.current().nextDouble()*2+pruductName.charAt(0);
    }
}
